package com.jamlu.framework.ui.status;

import android.content.Context;

import com.jamlu.framework.R;
import com.jamlu.framework.utils.ToastPts;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Toast状态布局
 * Created by DMing at 2018/2/27 0027
 */

public class StatusToast implements IStatus {

    private CompositeDisposable mCompositeDisposable;
    private Disposable mDisposable;//取消
    private Context mContext;

    StatusToast(Context mContext, CompositeDisposable mCompositeDisposable, StatusRequest statusRequest) {
        this.mContext = mContext;
        this.mCompositeDisposable = mCompositeDisposable;
        mDisposable = statusRequest.run(this);
        mCompositeDisposable.add(mDisposable);
    }

    @Override
    public void switchStatus(int status, Object... obj) {
        switch (status){
            case LOADING:
                break;
            case SUCCESS:
                break;
            case NOTHING:
                ToastPts.show(mContext.getString(R.string.status_nothing));
                break;
            case ERROR_RESPOND:
                if (obj != null && obj.length == 1) {
                    ToastPts.show(String.format("%s%s", mContext.getString(R.string.retry), obj[0]));
                }
                break;
            case ERROR_NETWORK:
                ToastPts.show(mContext.getString(R.string.status_error));
                break;
            case NO_NETWORK:
                ToastPts.show(mContext.getString(R.string.status_no_net));
                break;
        }
        releaseRef();
    }

    @Override
    public void releaseRef() {
        if (mDisposable != null && mCompositeDisposable != null)mCompositeDisposable.remove(mDisposable);
        mCompositeDisposable = null;
        mDisposable = null;
        mContext = null;
    }
}
